package com.leyou.page.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.*;
import com.leyou.itme.client.ItemClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageService {


    @Autowired
    private ItemClient itemClient;
    /*商品详情页面需要的字段：展示为 spu数据  参数：当然是：spu_id
    categories  商品分类集合 根据spu_cid3查询分类数据
    brand       品牌对象     根据spu_brandId查询品牌数据
    spuName     spu对象
    subTitle   spu对象
    detail    spu_detail对象 根据spu_id查询数据
    skus      sku集合
    specs     规格参数:规格参数与商品分类有关，可根据第三级分类id，先查询规格组数据
  整体数据采用map集合封装map<String,Object>
    * */

    public Map<String,Object> loadData(Long spuId) {

       Map<String,Object> map=new HashMap<>();
       /*根据spu_id查询spuDTO数据*/
       SpuDTO spuDTO= itemClient.findSpuById(spuId);
       map.put("spuName",spuDTO.getName());
       map.put("subTitle",spuDTO.getSubTitle());
       /*根据分类ids查询分类集合*/
        List<CategoryDTO> categoryDTOList = itemClient.findCategoryByIds(spuDTO.getCategoryIds());
        map.put("categories",categoryDTOList);
        /*根据spu_brandId查询品牌数据*/
        BrandDTO brand = itemClient.findBrandById(spuDTO.getBrandId());
        map.put("brand",brand);
        /*根据spu_id查询数据spu_detail*/
        SpuDetailDTO spuDetailDTO = itemClient.findSpuDetailBySupId(spuId);
        map.put("detail",spuDetailDTO);
        /*根据spu_id查询skus数据*/
        List<SkuDTO> skuDTOList = itemClient.findSkuBySpuId(spuId);
        map.put("skus",skuDTOList);
       /*根据第三级商品分类id查询规格组数据：一对多，List<SpecGroupDTO>*/
       /* 根据categoryId查询规格参数组和组内参数*/
        List<SpecGroupDTO> specGroupDTOList= itemClient.findSpecGroupDTO(spuDTO.getCid3());
        map.put("specs",specGroupDTOList);
        return map;
    }

    @Autowired
    private TemplateEngine templateEngine;

    /*创建静态模板*/
    public void createHTML(Long spuId){

        /*创建上下文*/
        Context context = new Context();
        //加载数据
        context.setVariables(this.loadData(spuId));
        /*将文件存入nginx目录下*/
        String filePath="D:\\Pensenol\\nginx-1.14.0\\html\\item";
        /*创建文件*/
        File htmlFile=new File(filePath,spuId+".html");
        try {
            /*写入*/
            PrintWriter writer=new PrintWriter(htmlFile);
            templateEngine.process("item", context,writer );
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new LyException(ExceptionEnum.DIRECTORY_WRITER_ERROR);
        }

    }

    public void deleteHTML(Long spuId) {

        /*删除文件*/
        String filePath="D:\\Pensenol\\nginx-1.14.0\\html\\item";
        /*创建文件*/
        File htmlFile=new File(filePath,spuId+".html");
        /*删除*/
        htmlFile.delete();
    }
}
